#!/bin/bash
# 2017-03-22
# ddy
# update bxn
# 必须在master上执行

. /usr/local/bin/logger
datetime=`date +"%Y%m%d%H%M%S"`
test ! -n "${1}" && logger error "module name is null ！eg. sh update.sh bxn-core-uic" && exit -1

moduleName=$1

test ! -d /var/nas/apps/$moduleName && logger error "$moduleName is not found in apps." && exit -1

cd $(cd `dirname $0`; pwd)
test ! -f $moduleName.war && logger error "$moduleName.war is not found." && exit -1
mv /var/nas/apps/$moduleName/6.2.2 /var/nas/apps/$moduleName/6.2.2_$datetime
test ! -f /usr/bin/unzip && yum -y install unzip
unzip -q $moduleName.war -d /var/nas/apps/$moduleName/6.2.2

if [ -d "script" ]; then
	#script 复制文件到data/update目录下
	updateFilePath="data/update/$moduleName-$datetime"
	mkdir -p /var/nas/$updateFilePath
	cp -r script $updateFilePath/
	sqls=`ls script`
	for sql in sqls; do
		echo "mysql -uroot -pbxn500 < /usr/local/$updateFilePath/$sql" >> /var/nas/$updateFilePath/update.sh
	done
	pods=`kubectl get pod | grep app | awk '{print $1}'`
	for pod in pods; do
		# sql
		kubectl exec $pod /bin/bash /usr/local/$updateFilePath/$update.sh
	done
	
fi
declare -A map=(["bxn-core-uic"]="uic" ["bxn-core-platform"]="platform.properties" ["bxn-timetable"]="timetable.properties" ["bxn-portal"]="portal.properties" ["bxn-score"]="score.properties")  
if [ -d "config" ]; then
	#config 仅把更新字段填入
	if [ -f "config/update.properties" ]; then
		updateContent=`cat config/update.properties`
		configName="${map[$moduleName]}"
		test -z "$configName" && configName="$moduleName.properties"
		configfiles=`ls /var/nas/*/config/$configName`
		if [ ! -z "configfiles"]; then
			for configfile in configfiles; do
				echo "$updateContent" >> $configfile
			done
		else
		# 未发现此配置文件
			logger debug "configfile $configName not found!"
		fi
	fi
fi






#